import React, { useEffect, useRef, useState } from 'react'
import ProForm, { ProFormText, } from '@ant-design/pro-form'
import { Card, message } from 'antd'
import { editWebConfig, getWebConfigDetail } from '@/services/webconfig'
import ProSkeleton from '@ant-design/pro-skeleton'

const Sms = () => {
  // 把获取到的数据存到状态
  const [configer, setConfiger] = useState(null)

  useEffect(async () => {
    //获取网站配置详情
    const configer = await getWebConfigDetail('sms').then(res => {
      return setConfiger(res.value)
    })

  }, [])

  /**
   * 修改网站配置
   * @param values 表单中输入的数据
   * @returns {Promise<void>}
   */
  const updataWebConfig = async values => {
    /**
     * 定义要修改的字段
     * @type {{cn_name: string, value: {bucket, secret_key, endpoint: *, access_key, is_name: (boolean|*)}}}
     */
    const data = {
      "cn_name": "短信设置",
      "value": {
        "sign_tem": values.sign_tem,
        "sign_name": values.sign_name,
        "access_key": values.access_key,
        "secret_key": values.secret_key
      }

    }
    /**
     * 执行修改
     * @type {*}
     */
    const response = await editWebConfig('sms', data)
    if (response.status === undefined) {
      message.success('修改成功')
    }
  }

  return (
    <div title="输入表单">
      {
        configer === null ?
          <div
            style={{
              background: '#fafafa',
              padding: 24,
            }}
          >
            <ProSkeleton type="list"/>
          </div> :
          <Card>
            <ProForm
              onFinish={values => updataWebConfig(values)}
              initialValues={configer}
              submitter={{
                // 配置按钮的属性
                resetButtonProps: {
                  style: {
                    // 隐藏重置按钮
                    display: 'none',
                  },
                },
              }
              }
            >
              <ProFormText
                name="sign_tem"
                label="短信模板"
                colon={true}
                tooltip="最长为 24 位"
                placeholder="请输入名称"
                rules={[{ required: true, message: '请输入短信模板' }]}
              />
              <ProFormText
                name="sign_name"
                label="短信签名"
                placeholder="请输入名称"
                rules={[{ required: true, message: '请输入短信签名' }]}
              />
              <ProFormText
                name="access_key"
                label="access_key"
                colon={true}
                tooltip="最长为 24 位"
                placeholder="请输入名称"
                rules={[{ required: true, message: '请输入access_key' }]}
              />

              <ProFormText
                name="secret_key"
                label="secret_key"
                colon={true}
                tooltip="最长为 24 位"
                placeholder="请输入名称"
                rules={[{ required: true, message: '请输入secret_key' }]}
              />
            </ProForm>
          </Card>
      }

    </div>
  )
}

export default Sms

